﻿
#include "GeoSearchViewController.h"

GeoSearchViewController::GeoSearchViewController()
{

}

GeoSearchViewController::~GeoSearchViewController()
{

}

void GeoSearchViewController::viewDidLoad()
{
    // Do any additional setup after loading the view from its nib.

	this->setNavigationBarItem(CANavigationBarItem::create(UTF8("地理编码")));

	const DSize& viewSize = this->getView()->getFrame().size;

	AppTool::getInstance()->setGeocodeDelegate(this);

	CAButton* geocodebt = CAButton::createWithFrame(DRect(50, 100, 200, 90), CAButtonTypeRoundedRect);
	geocodebt->setTitleFontSize(30);
	geocodebt->setTitleForState(CAControlStateAll, UTF8("正地理编码"));
	geocodebt->setTitleColorForState(CAControlStateAll, CAColor_black);
	geocodebt->addTarget(this, CAControl_selector(GeoSearchViewController::geocodeBtClicked), CAControlEventTouchUpInSide);
	this->getView()->addSubview(geocodebt);

	lb1 = CALabel::createWithFrame(DRect(20, 100 + 100, viewSize.width - 20 * 2, 300));
	lb1->setFontSize(30);
	lb1->setColor(CAColor_black);
	lb1->setTextAlignment(CATextAlignmentLeft);
	lb1->setVerticalTextAlignmet(CAVerticalTextAlignmentCenter);
	lb1->setText(UTF8("正地理编码"));
	this->getView()->addSubview(lb1);

	CAButton* regeocodebt = CAButton::createWithFrame(DRect(50, 100 + 100 + 300, 200, 90), CAButtonTypeRoundedRect);
	regeocodebt->setTitleFontSize(30);
	regeocodebt->setTitleForState(CAControlStateAll, UTF8("反地理编码"));
	regeocodebt->setTitleColorForState(CAControlStateAll, CAColor_black);
	regeocodebt->addTarget(this, CAControl_selector(GeoSearchViewController::regeocodeBtClicked), CAControlEventTouchUpInSide);
	this->getView()->addSubview(regeocodebt);

	lb2 = CALabel::createWithFrame(DRect(20, 100 + 100 + 300 + 100, viewSize.width - 20 * 2, 300));
	lb2->setFontSize(30);
	lb2->setColor(CAColor_black);
	lb2->setTextAlignment(CATextAlignmentLeft);
	lb2->setVerticalTextAlignmet(CAVerticalTextAlignmentCenter);
	lb2->setText(UTF8("反地理编码"));
	this->getView()->addSubview(lb2);
	
}

void GeoSearchViewController::viewDidUnload()
{
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}

void GeoSearchViewController::geocodeBtClicked(CAControl* ctrl, DPoint point)
{
	AppTool::getInstance()->setGeocodeDelegate(this);
	AppTool::getInstance()->getGeocode(UTF8("玄武门"), UTF8("南京"));
}

void GeoSearchViewController::regeocodeBtClicked(CAControl* ctrl, DPoint point)
{
	AppTool::getInstance()->setGeocodeDelegate(this);
	AppTool::getInstance()->getReGeocode(39.976587, 116.339925, 1000);
}

void GeoSearchViewController::onGetGeocodeAddress(int code, std::vector<GeocodeAddress> v_addrs)
{
	m_iCode = code;
	v_GeocodeAddress = v_addrs;

	CAScheduler::schedule(schedule_selector(GeoSearchViewController::geocodeSchedule), this, 0);
}

void GeoSearchViewController::geocodeSchedule(float t)
{
	CAScheduler::unschedule(schedule_selector(GeoSearchViewController::geocodeSchedule), this);

	if (m_iCode == 0 && v_GeocodeAddress.size() > 0)
	{
		lb1->setText(UTF8("正地理编码结果：") + crossapp_format_string("\nnum:%d\naddress:%s", v_GeocodeAddress.size(), v_GeocodeAddress[0].formatAddr.c_str()));
		CCLog("\ncode:%d \nlat:%f \nlng:%f \nformatAddr:%s \nprovince:%s \ncity:%s \ndistrict:%s \ntown:%s \nstreet:%s \nadcode:%s", m_iCode, v_GeocodeAddress[0].lat, v_GeocodeAddress[0].lng, v_GeocodeAddress[0].formatAddr.c_str(), v_GeocodeAddress[0].province.c_str(), v_GeocodeAddress[0].city.c_str(), v_GeocodeAddress[0].district.c_str(), v_GeocodeAddress[0].town.c_str(), v_GeocodeAddress[0].street.c_str(), v_GeocodeAddress[0].adcode.c_str());
	}
	else if (m_iCode == 0 && v_GeocodeAddress.size() == 0)
	{
		lb1->setText(UTF8("正地理编码结果：无返回结果"));
	}
	else if (m_iCode != 0)
	{
		lb1->setText(UTF8("正地理编码结果：失败"));
	}
}

void GeoSearchViewController::onGetRegeocodeInfo(int code, RegeocodeInfo info)
{
	regeocodeInfo = info;
	m_iCode = code;

	CAScheduler::schedule(schedule_selector(GeoSearchViewController::regeocodeSchedule), this, 0);
}

void GeoSearchViewController::regeocodeSchedule(float t)
{
	CAScheduler::unschedule(schedule_selector(GeoSearchViewController::regeocodeSchedule), this);

	if (m_iCode == 0 )
	{
		lb2->setText(UTF8("反地理编码结果：") + crossapp_format_string("\naddress:%s", regeocodeInfo.formatAddr.c_str()));
		CCLog("\ncode:%d \nformatAddr:%s \nprovince:%s \ncity:%s \ndistrict:%s \ntown:%s \nstreet:%s \nadcode:%s \npoiNum:%d", m_iCode, regeocodeInfo.formatAddr.c_str(), regeocodeInfo.province.c_str(), regeocodeInfo.city.c_str(), regeocodeInfo.district.c_str(), regeocodeInfo.town.c_str(), regeocodeInfo.street.c_str(), regeocodeInfo.adcode.c_str(), (int)regeocodeInfo.v_Pois.size());
	}
	else
	{
		lb2->setText(UTF8("反地理编码结果：失败"));
	}
}

